/*
 * File Name：TrackPhoto.java
 * Copyright：Copyright 2008-2012 CiWong.Inc. All Rights Reserved.
 * Description： TrackPhoto.java
 * Modify By：jejun
 * Modify Date：2012-12-9
 * Modify Type：Add
 */
package com.ciwong.ciwongwrite.mode;

import java.util.ArrayList;
import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

/**
 * 添加图片
 *
 * @author jejun
 * @version ciwong v.1.0 2012-12-9
 * @since ciwong v.1.0
 */
public class TrackPhoto extends TrackMove
{

    private static final StringBuilder builder = new StringBuilder();

    private PhotoMode mPhotoMode;

    private long photoName;

    private Bitmap mBitmap;

    private float x;

    private float y;

    private int width;

    private int height;

    /**
     * 解析鼠标操作
     *
     * @param photoActions
     * @return
     */
    public static List<TrackPhoto> readTrackPhoto(String photoActions,
                                                  String dataSource)
    {
        String[] arr = photoActions.split(";");
        int len = arr.length;
        List<TrackPhoto> trackPhotos = new ArrayList<TrackPhoto>();
        for (int i = 0; i < len; i++)
        {
            final TrackPhoto mTrackMouse = new TrackPhoto();
            mTrackMouse.mPhotoMode = reflectAction(arr[i], dataSource);
            trackPhotos.add(mTrackMouse);
        }
        return trackPhotos;
    }

    private static PhotoMode reflectAction(String action, String dataSource)
    {
        final PhotoMode mPhotoMode = new PhotoMode();
        String[] arr = action.replaceAll("[\\(\\)]", "").split(",");
        if (arr.length == 5)
        {
            mPhotoMode.x = Float.parseFloat(arr[0]);
            mPhotoMode.y = Float.parseFloat(arr[1]);
            final String path = dataSource + "/photo/" + arr[2];
            mPhotoMode.photo = BitmapFactory.decodeFile(path);
            mPhotoMode.width = Integer.parseInt(arr[3]);
            mPhotoMode.height = Integer.parseInt(arr[4]);
        }
        return mPhotoMode;
    }

    public TrackPhoto()
    {

    }

    public TrackPhoto(Bitmap mBitmap, long photoName, float x, float y,
                      int width, int height)
    {
        this.mBitmap = mBitmap;
        this.photoName = photoName;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    @Override
    public void move(float x, float y)
    {

    }

    @Override
    public void touchUp(Canvas mCanvas)
    {

    }

    @Override
    public void touchDown(Canvas mCanvas, float x, float y)
    {

    }

    @Override
    public void draw(Canvas mCanvas)
    {
        if (!isRecord)
        {
            if (mPhotoMode.photo != null)
            {
                TrackBase.mCanvas.drawBitmap(mPhotoMode.photo, 0, 0, null);
                mPhotoMode.photo.recycle();
                mPhotoMode.photo = null;
            }
        }
        else
        {
            if (mBitmap != null)
            {
                mCanvas.drawBitmap(mBitmap, 0, 0, null);
            }
        }
    }

    @Override
    public String format(boolean touchDown)
    {
        builder.delete(0, builder.length());
        builder.append("(");
        builder.append((int) x);
        builder.append("," + (int) y);
        builder.append("," + photoName);
        builder.append("," + width);
        builder.append("," + height);
        builder.append(")");
        return builder.toString();
    }

    private static class PhotoMode extends TrackMove.MoveMode
    {
        public Bitmap photo;

        public int width;

        public int height;
    }

}
